import { IItemConfig } from "../../../Script/Base/BaseConfig";
import BaseView from "../../../Script/Base/BaseView";
import { EBagType, EBundleType, EResType, EViewType } from "../../../Script/Common/Enum";
import EventID from "../../../Script/Common/EventID";
import { GlobalData } from "../../../Script/Common/GlobalData";
import { IBagItemData, IMessage, ITalkData, IViewData } from "../../../Script/Interface/IMessage";
import AppService from "../../../Script/Service/AppService";
import { LoadUtil } from "../../../Script/Util/LoadUtil";
import { SceneGame } from "../../SceneGame";
import StorageUtil from "../../Util/StorageUtil";
import ItemBag from "./ItemBag";

const { ccclass, property } = cc._decorator;

const VIEW_DATA: IViewData = {
    name: 'BagUI',
    type: EViewType.Panel,
    bundle: EBundleType.Game,
    zIndex: 100,
};
@ccclass
export default class BagUI extends BaseView implements IMessage {
    public static instance: BagUI = null;

    @property(cc.Prefab)
    private itemBag: cc.Prefab = null;

    @property(cc.ScrollView)
    private scrollView: cc.ScrollView = null;

    @property(cc.Node)
    private content: cc.Node = null;

    @property([cc.Node])
    private tags: cc.Node[] = [];

    @property(cc.Node)
    private infoView: cc.Node = null;

    private items: cc.Node[] = [];
    private selectIndex: number = 0;

    private bagData: IBagItemData[] = [];
    
    recvMsg(cmd: number, data: any): void {
        switch (cmd) {
            case EventID.BAG_LIGHT_ITEM:
                for (let i = 0; i < this.items.length; i++) {
                    const comp = this.items[i].getComponent(ItemBag);
                    if (comp.getData().id === data.id) {
                        SceneGame.instance.showGuideUI(this.items[i], 1);
                        break;
                    }
                }
                break;
        }
    }

    protected onLoad() {
        super.onLoad();
        this.addMessage(EventID.BAG_LIGHT_ITEM, this);

        BagUI.instance = this;

        this.bagData = StorageUtil.instance.getBagData();

        this.initItems();
        this.refreshUI();
    }

    private initItems() {
        console.log("BagUI.initItems()===>bagData:", this.bagData);
        
        this.bagData.sort((o1, o2) => { return o1.id - o2.id });
        this.bagData.forEach((o) => {
            const item = cc.instantiate(this.itemBag);
            item.parent = this.content;
            item.getComponent(ItemBag).setData(o, this.prepareData?.type || "地摊");
            this.items.push(item);
        });
    }

    private refreshUI() {
        this.infoView.active = false;

        this.tags.forEach((o, i) => {
            const select = cc.find("select", o);
            select.active = i === this.selectIndex;
        });

        this.items.forEach((o)=>{
            const comp = o.getComponent(ItemBag);
            const type = comp.getType();
            if (this.selectIndex === EBagType.All) {
                o.active = type !== EBagType.Store;
            } else {
                o.active = this.selectIndex == type;
            }
        });
    }

    public showItemInfo(config: IItemConfig, itemData: IBagItemData) {
        this.isHasChild = true;
        this.infoView.active = true;
        const name = cc.find("bg/name", this.infoView).getComponent(cc.Label);
        const desc = cc.find("bg/desc", this.infoView).getComponent(cc.Label);
        const price = cc.find("bg/price", this.infoView).getComponent(cc.Label);
        const num = cc.find("bg/item/num", this.infoView).getComponent(cc.Label);
        const icon = cc.find("bg/item/icon", this.infoView).getComponent(cc.Sprite);
        name.string = config.name;
        desc.string = config.name;
        price.string = `售价: ${config.basePrice}`;
        num.string = `${itemData.num}`;
        LoadUtil.instance.setSpriteFrame(icon, EResType.Item, config.id, 120, 120);
    }

    private onTagBtnClicked(event, eventData) {
        const index = Number(eventData);
        if (this.selectIndex === index) return;
        this.selectIndex = index;
        this.refreshUI();
    }

    public onCloseChild() {
        if (this.isHasChild) {
            this.isHasChild = false;
            this.infoView.active = false;
        }
    }
}
AppService.instance.registViewData(VIEW_DATA);